package cn.iocoder.yudao.module.pay.bill.controller.admin;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.pay.bill.convert.PayBillConvert;
import cn.iocoder.yudao.module.pay.bill.dal.dataobject.PayBillDO;
import cn.iocoder.yudao.module.pay.bill.service.PayBillService;
import cn.iocoder.yudao.module.pay.bill.vo.PayBillPageReqVO;
import cn.iocoder.yudao.module.pay.bill.vo.PayBillRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 账单")
@RestController
@RequestMapping("/pay/bill")
@Validated
public class PayBillController {

    @Resource
    private PayBillService payBillService;

    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/page")
    @Operation(summary = "获得账单分页")
    @PreAuthorize("@ss.hasPermission('pay:bill:query')")
    public CommonResult<PageResult<PayBillRespVO>> getBillPage(@Valid PayBillPageReqVO pageVO) {
        PageResult<PayBillDO> pageResult = payBillService.getBillPage(pageVO);
        PageResult<PayBillRespVO> data = PayBillConvert.INSTANCE.convertPage(pageResult);
        Set<Long> userIds = convertSet(data.getList(), PayBillRespVO::getUserId);
        Map<Long, UserSimpleVO> userMap = memberUserApi.getUserSimpleMap(userIds);
        data.getList().forEach(item -> item.setUser(userMap.get(item.getUserId())));
        return success(data);
    }

}
